<?php
namespace app\admin\controller;
use app\admin\model\Banner as BannerModel;
use app\admin\controller\Base;//权限验证
use think\Loader;
use think\Session;
class Banner extends Base{
	
    public function index(){
		$model= new BannerModel();
		//栏目分页
		$list = BannerModel::paginate(5);
		$this->assign('list',$list);
		return $this->fetch('list');
	}
	
	public function add(){
    	//添加banner图
    	if(request()->isPost()){
            // dump($_POST); die;
			$data=[
    			'title'=>input('title'),
    			'addtime'=>date("Y-m-d H:i:s"),
            ];

            if($_FILES['picture']['tmp_name']){
                $file = request()->file('picture');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/admin/uploads');
                $data['picture']='/uploads/'.$info->getSaveName();
            }

    		if(db('Banner')->insert($data)){
    			return $this->success('添加banner图成功！','index');
    		}else{
    			return $this->error('添加banner图成功！');
    		}
    		return;
    	}
        return $this->fetch();
    }
	
	
	
	public function edit(){
		//Banner图修改
		$id=input('id');
		$banners=db('banner')->find($id);
		if(request()->isPost()){
			$data=[
				'id'=>input('id'),
				'title'=>input('title'),
				'addtime'=>date("Y-m-d H:i:s"),
			];
			if($_FILES['picture']['tmp_name']){
			    $file = request()->file('picture');
			    $info = $file->move(ROOT_PATH . 'public' . DS . 'static/admin/uploads');
			    $data['picture']='/uploads/'.$info->getSaveName();
			}
			
	        $save=db('banner')->update($data);
			if($save !== false){
				$this->success('修改Banner图成功！','index');
			}else{
				$this->error('修改Banner图失败！');
			}
			return;
		}
		$this->assign('banners',$banners);
		return $this->fetch('edit');
	}
    
    public function del(){
    	//Banner图删除
    	$id=input('id');
    	
		if(db('banner')->delete(input('id'))){
			$this->success('Banner图删除成功！','index');
		}else{
			$this->error('Banner图删除失败！');
		}
    }	
}